//
//  AppDelegate.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "LessonViewController.h"
#import "ShikeViewController.h"
#import "ShipuViewController.h"
#import "UserViewController.h"
#import "TopicViewController.h"
#import "ChurenhuiViewController.h"
#import "UMSocial.h"
#import "GuideViewController.h"
#import "SetViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UMSocialData setAppKey:@"56a881e5e0f55ad4a0000581"];
    
    [SMSSDK registerApp:@"f73acfa6e7f0"
             withSecret:@"4996e6f13a54f495bfd5fa1457835c7b"];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:@"isFirst"] == nil) {
        GuideViewController *guideVC = [[GuideViewController alloc] init];
        _window.rootViewController = guideVC;
        [userDefault setObject:@"已经启动过程序了" forKey:@"isFirst"];
        NSLog(@"第一次启动程序");
    
    [_window release];
    
    }
    else
    { NSLog(@"已经启动过了");
        
        [NSThread sleepForTimeInterval:2.0];
    //食谱页
    ShipuViewController *shipu = [[ ShipuViewController alloc] init];
    UINavigationController *shipuNavi = [[ UINavigationController alloc] initWithRootViewController:shipu];
    UITabBarController *tabbar = [[ UITabBarController alloc] init];
    shipu.tabBarItem = [[ UITabBarItem alloc] initWithTitle:@"食谱" image:[UIImage imageNamed:@"shipinlineA@2x"] selectedImage:[UIImage imageNamed:@"shipinlineB@2x"]];
    
    
    //社区页
    TopicViewController *topic = [[ TopicViewController alloc] init];
    UINavigationController *topicNavi =[[ UINavigationController alloc] initWithRootViewController:topic];
    topic.tabBarItem =[[ UITabBarItem alloc] initWithTitle:@"下厨房" image:[UIImage imageNamed:@"iconfont-fangzi@2x"] selectedImage:[UIImage imageNamed:@"iconfont-fangzi-2@2x"]];
    //食课页
    LessonViewController *lesson = [[ LessonViewController alloc] init];
    UINavigationController *lessonNavi = [[ UINavigationController alloc] initWithRootViewController:lesson];
    lesson.tabBarItem =[[ UITabBarItem alloc] initWithTitle:@"学做菜" image:[UIImage imageNamed:@"iconfont-shu@2x"] selectedImage:[UIImage imageNamed:@"iconfont-shu-2@2x"]];
    //用户页
    UserViewController *user = [[ UserViewController alloc] init];
    UINavigationController *userNAvi =[[ UINavigationController alloc] initWithRootViewController:user];
    user.tabBarItem =[[ UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"chushimaoA@2x"] selectedImage:[UIImage imageNamed:@"chushimaoB@2x"]];
    
    //厨人汇
    ChurenhuiViewController *people = [[ ChurenhuiViewController alloc] init];
    UINavigationController *peopleNavi = [[UINavigationController alloc] initWithRootViewController:people];
    people.tabBarItem = [[ UITabBarItem alloc] initWithTitle:@"厨友汇" image:[UIImage imageNamed:@"iconfont-shequ@2x"] selectedImage:[UIImage imageNamed:@"iconfont-shequ-2@2x"]];
    
    tabbar.viewControllers = @[shipuNavi,topicNavi,lessonNavi,peopleNavi,userNAvi];
    self.window.rootViewController = tabbar;
    
    [shipu release];
    [shipuNavi release];
    [topic release];
    [topicNavi release];
    [lesson release];
    [lessonNavi release];
    [userNAvi release];
    [user release];
    }
return YES;

}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    //取消徽章
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"noti:%@",notification);
    
    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"
                                                    message:notMess
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    // 在不需要再推送时，可以取消推送
    [SetViewController cancelLocalNotificationWithKey:@"key"];
}

@end
